<template>
  <div class="row col-12">
    <div class="col-6">
      <table class="table table-sm">
        <caption>
          职位级别信息
        </caption>
        <thead>
          <tr>
            <th>级别ID</th>
            <th>级别代码</th>
            <th>级别名称</th>
            <th>VIP等级</th>
            <th></th>
          </tr>
        </thead>
        <tr v-for="info of dataList">
          <td>{{ info.positionId }}</td>
          <td>{{ info.positionCode }}</td>
          <td>{{ info.name }}</td>
          <td>{{ info.vipLevel }}</td>
          <td>
            <button
              class="btn btn-sm btn-danger"
              @click.stop="deletePosition(info.id)"
            >
              删除
            </button>
          </td>
        </tr>
      </table>
    </div>
    <div class="col-6 bg-light">
      <form class="form-horizontal">
        <div class="row mb-2">
          <label class="control-label col-3 text-end"
            ><strong>级别ID</strong></label
          >
          <div class="col-6">
            <input
              type="input"
              class="form-control"
              v-model.number="positionId"
            />
            <span class="small text-muted">自然数，值越大表示级别越高</span>
          </div>
        </div>
        <div class="row mb-2">
          <label class="control-label col-3 text-end"
            ><strong>级别名称</strong></label
          >
          <div class="col-6">
            <input type="input" class="form-control" v-model="name" />
            <span class="small text-muted"></span>
          </div>
        </div>
        <div class="row mb-2">
          <label class="control-label col-3 text-end">级别代码</label>
          <div class="col-6">
            <input type="input" class="form-control" v-model="positionCode" />
            <span class="small text-muted">选填</span>
          </div>
        </div>
        <div class="row mb-2">
          <label class="control-label col-3 text-end">VIP级别</label>
          <div class="col-6">
            <input
              type="input"
              class="form-control"
              v-model.number="vipLevel"
            />
            <span class="small text-muted"
              >默认为0，非VIP；VIP级别从1-10，数字越大，级别越高</span
            >
          </div>
        </div>
        <div class="row mb-2">
          <div class="col-6 offset-3 form-check">
            <input
              type="checkbox"
              class="form-check-input"
              v-model="keepOpening"
            />
            <label class="form-check-label">保存后不清空</label>
          </div>
        </div>
        <div class="row">
          <button
            id="btnChange"
            type="button"
            class="btn btn-primary  col-4 offset-4"
            @click.stop="savePosition()"
          >
            保存
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import {
  searchCustomerPositions,
  searchCustomerFlightDiscounts,
  saveCustomerPosition,
} from "@/api/customer.js";

import MyModalPrompt from "@/components/modal/my-modal-prompt.vue";

export default {
  props: {
    customerId: {
      type: Number,
    },
  },
  components: {
    MyModalPrompt,
  },
  data() {
    return {
      modalTitle: "",
      saving: false,

      dataList: [],
      positionId: 0,
      name: "",
      positionCode: "",
      vipLevel: 0,

      keepOpening: false,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (msg) {
      this.$store.dispatch("showLoading", { loadingText: msg });
    },
    hideLoading: function () {
      this.$store.dispatch("hideLoading");
    },
    search: function () {
      searchCustomerPositions(this.customerId, (v) => {
        this.dataList = v.dataList;
      });
    },
    savePosition: function () {
      const params = {
        positionId: this.positionId,
        name: this.name,
        positionCode: this.positionCode,
        vipLevel: this.vipLevel,
      };

      const jsonParam = JSON.stringify(params);

      saveCustomerPosition(this.customerId, jsonParam, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("保存成功");
          this.search();
          if (this.keepOpening === false) {
            this.reset();
          }
        } else {
          this.showErrMsg("保存失败：" + v.errmsg, "danger");
        }
      });
    },
    deletePosition: function (id) {
      const params = {
        mode: 4,
        id: id,
      };
      saveCustomerPosition(this.customerId, JSON.stringify(params), (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败：" + v.errmsg, "danger");
        }
      });
    },
    reset: function () {
      this.positionId = 0;
      this.name = "";
      this.positionCode = "";
      this.vipLevel = 0;
    },
  },
};
</script>